﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace HealthCommunity
{
	public class CommonHelper
	{
		/// <summary>
		/// Verifies that a string is in valid e-mail format
		/// </summary>
		/// <param name="email">Email to verify</param>
		/// <returns>true if the string is a valid e-mail address and false if it's not</returns>
		public static bool IsValidEmail(string email)
		{
			bool result = false;
			if (String.IsNullOrEmpty(email))
				return result;
			email = email.Trim();
			result = System.Text.RegularExpressions.Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
			return result;
		}

		/// <summary>
		/// Gets query string value by name
		/// </summary>
		/// <param name="name">Parameter name</param>
		/// <returns>Query string value</returns>
		public static string QueryString(string name)
		{
			string result = string.Empty;
			if (HttpContext.Current != null && HttpContext.Current.Request.QueryString[name] != null)
				result = HttpContext.Current.Request.QueryString[name].ToString();
			return result;
		}

		/// <summary>
		/// Gets boolean value from query string 
		/// </summary>
		/// <param name="name">Parameter name</param>
		/// <returns>Query string value</returns>
		public static bool QueryStringBool(string name)
		{
			string resultStr = QueryString(name).ToUpperInvariant();
			return (resultStr == "YES" || resultStr == "TRUE" || resultStr == "1");
		}

		/// <summary>
		/// Gets integer value from query string 
		/// </summary>
		/// <param name="name">Parameter name</param>
		/// <returns>Query string value</returns>
		public static int QueryStringInt(string name)
		{
			string resultStr = QueryString(name).ToUpperInvariant();
			int result;
			Int32.TryParse(resultStr, out result);
			return result;
		}

		/// <summary>
		/// Gets integer value from query string 
		/// </summary>
		/// <param name="name">Parameter name</param>
		/// <param name="defaultValue">Default value</param>
		/// <returns>Query string value</returns>
		public static int QueryStringInt(string name, int defaultValue)
		{
			string resultStr = QueryString(name).ToUpperInvariant();
			if (resultStr.Length > 0)
			{
				return Int32.Parse(resultStr);
			}
			return defaultValue;
		}

		public static List<int> QueryStringInts(string name)
		{
			var qstr = QueryString(name);

			List<int> result = new List<int>();

			if(!string.IsNullOrEmpty(qstr))
			{
				if(qstr.Contains(' '))
				{
					var qstrItems = qstr.Split(' ');

					foreach(var item in qstrItems)
					{
						result.Add(int.Parse(item));
					}
				}
				else
				{
					result.Add(QueryStringInt(name));
				}
			}

			return result;
		}

		/// <summary>
		/// Gets GUID value from query string 
		/// </summary>
		/// <param name="name">Parameter name</param>
		/// <returns>Query string value</returns>
		public static Guid? QueryStringGuid(string name)
		{
			string resultStr = QueryString(name).ToUpperInvariant();
			Guid? result = null;
			try
			{
				result = new Guid(resultStr);
			}
			catch
			{
			}
			return result;
		}

		public static void SendMail(string to, string subject, string content)
		{
			MailMessage m = new MailMessage();
			m.Subject = subject;
			m.To.Add(to);
			m.Body = content;

			SmtpClient c = new SmtpClient();

			try
			{
				c.Send(m);
			}
			catch (SmtpException smtpexc)
			{
				throw smtpexc;
			}
			catch (Exception exc)
			{
				throw exc;
			}
		}
	}
}